﻿using Application.Features.Menus.Commands.Add;
using Application.Features.Menus.Commands.Delete;
using Application.Features.Menus.Commands.Update;
using Application.Features.Menus.DTOs;
using Application.Features.Menus.Queries.GetAll;
using Common.Models;
using Microsoft.AspNetCore.Mvc;
using System.ComponentModel;

namespace WebAPI.Controllers;

/// <summary>
/// 菜单管理
/// </summary>
[Description("菜单管理")]
public class MenuController : ApiControllerBase
{
    /// <summary>
    /// 全部查询
    /// </summary>
    /// <returns></returns>
    [HttpGet("query/all")]
    [Description("全部查询")]
    public async Task<Result<IEnumerable<MenuOutput>>> GetAllMenusQueryAsync()
    {  
        return await Result<IEnumerable<MenuOutput>>.SucceedAsync(await Mediator.Send(new GetAllMenusQuery()));
    }

    /// <summary>
    /// 菜单列表
    /// </summary>
    /// <returns></returns>
    [HttpGet("query")]
    [Description("菜单列表")]
    public async Task<Result<IEnumerable<MenuOutput>>> GetMenusQueryAsync()
    {
        return await Result<IEnumerable<MenuOutput>>.SucceedAsync(await Mediator.Send(new GetMenusQuery()));
    }

    /// <summary>
    /// 创建
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    [Description("创建")]
    public async Task<Result> AddAsync(AddMenuCommand command)
    {
        await Mediator.Send(command);
        return await Result.SucceedAsync();
    }

    /// <summary>
    /// 修改
    /// </summary>
    /// <returns></returns>
    [HttpPut("{id}")]
    [Description("修改")]
    public async Task<Result> UpdateAsync(long id, UpdateMenuCommand command)
    {
        await Mediator.Send(command);
        return await Result.SucceedAsync();
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="ids">删除多个请用英文下的逗号,分开</param>
    /// <returns></returns>
    [HttpDelete("delete/{ids}")]
    [Description("删除")]
    public async Task<Result> DeleteAsync(string ids)
    {
        await Mediator.Send(new DeleteMenuCommand { MenuIds = ids.Split(',').Select(long.Parse).ToList() });
        return await Result.SucceedAsync();
    }

    /// <summary>
    /// 同步接口
    /// </summary>
    /// <returns></returns>
    [HttpPut("execute/webapi")]
    [Description("同步接口")]
    public async Task<Result> ExecuteSyncAPICommandAsync()
    {
        await Mediator.Send(new ExecuteSyncAPICommand());
        return await Result.SucceedAsync();
    }

    /// <summary>
    /// 备份
    /// </summary>
    /// <returns></returns>
    [HttpPut("backup/to/json/file")]
    [Description("备份菜单")]
    public async Task<Result> BackupMenuDataToJsonCommandAsync()
    {
        await Mediator.Send(new BackupMenuDataToJsonCommand());
        return await Result.SucceedAsync();
    }
}
